package team.stride.database.crud.music;

import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.database.crud.music.MusicItem;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class BDMusicActivity extends Activity
{
	static final int MODIFY_DIALOG = 0;
	private List<MusicItem> recordsList;
	private ListView listLV;
	private MusicAdapter adapter;
	private Dialog modifDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bd_music_layout);
		initDialog();
		listLV = (ListView) findViewById(R.id.musicListView);

		recordsList = new ArrayList<MusicItem>();
		adapter = new MusicAdapter(this, recordsList);
		listLV.setAdapter(adapter);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		recordsList.clear();
		Cursor cursor = managedQuery(StrideDB.CONTENT_URI_MUSIC, null, null, null, null);
		while (cursor.moveToNext())
		{
			final int id_index = cursor.getColumnIndex(StrideDB.ID_M);
			final int song_index = cursor.getColumnIndex(StrideDB.SONG_M);
			final int singer_index = cursor.getColumnIndex(StrideDB.SINGER_M);
			final int playlist_index = cursor.getColumnIndex(StrideDB.PLAYLIST_M);
			final int file_index = cursor.getColumnIndex(StrideDB.FILE_M);
			final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_M);

			int id = cursor.getInt(id_index);
			String song = cursor.getString(song_index);
			String singer = cursor.getString(singer_index);
			String playlist = cursor.getString(playlist_index);
			String file = cursor.getString(file_index);
			String details = cursor.getString(details_index);

			recordsList.add(new MusicItem(id, song, singer, playlist, file, details));
		}
		cursor.close();
		adapter.notifyDataSetChanged();
	}
	

	private void initDialog()
	{
		modifDialog = new Dialog(this);
		modifDialog.setContentView(R.layout.bd_music_modify);
		modifDialog.setTitle("Modify a Music Entry");
		modifDialog.setCancelable(true);
		modifDialog.setCanceledOnTouchOutside(true);
	}
	
	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch (id)
		{
		case MODIFY_DIALOG:
			return modifDialog;
		}
		return null;
	}

	public void showDialog(int modifyDialog, MusicItem entry)
	{
		refreshDialog(entry);
		showDialog(MODIFY_DIALOG);
	}

	private void refreshDialog(final MusicItem entry)
	{
		final EditText song = (EditText) modifDialog.findViewById(R.id.musicModifySong);
		song.setText(entry.getSong());
		
		final EditText singer = (EditText) modifDialog.findViewById(R.id.musicModifySinger);
		singer.setText(entry.getSinger());
		
		final EditText file = (EditText) modifDialog.findViewById(R.id.musicModifyFile);
		file.setText(entry.getDetails());

		final EditText playlist = (EditText) modifDialog.findViewById(R.id.musicModifyPlaylist);
		playlist.setText(entry.getDetails());
		
		final EditText details = (EditText) modifDialog.findViewById(R.id.musicModifyDetails);
		details.setText(entry.getDetails());
		
		Button okBtn = (Button) modifDialog.findViewById(R.id.musicModifyOk);
		okBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				entry.setFile(file.getText().toString());
				entry.setSong(song.getText().toString());
				entry.setSinger(singer.getText().toString());
				entry.setPlaylist(playlist.getText().toString());
				entry.setDetails(details.getText().toString());
				adapter.notifyDataSetChanged();
				
				final ContentResolver cr = v.getContext().getContentResolver();
				ContentValues values = new ContentValues();
				values.put(StrideDB.SONG_M, song.getText().toString());
				values.put(StrideDB.SINGER_M, singer.getText().toString());
				values.put(StrideDB.FILE_M, file.getText().toString());
				values.put(StrideDB.PLAYLIST_M, playlist.getText().toString());
				values.put(StrideDB.DETAILS_M, details.getText().toString());
				cr.update(StrideDB.CONTENT_URI_MUSIC, values, StrideDB.ID_M + " = " + entry.getId(), null);
				modifDialog.dismiss();
			}
		});
		
	}
	
}